/*
 * FirstSuitableSched.java
 *
 * Created on Streda, 2007, november 14, 7:59
 *
 */

package logic.schedulers;

import gridsim.ResourceCharacteristics;
import java.util.Collection;
import java.util.Vector;
import javax.swing.JFrame;
import logic.gridSimInterface.resource.ResourceCharacteristicExt;
import logic.gridSimInterface.user.GridletInfo;
import logicTests.Dbg;

/**
 * Algoritmus pridelovania zdrojov uloham v gride. Najskor hlada prvy
 * vhodny volny zdroj. Ak taky neexistuje priraduje prvy vhodny zdroj.
 * Rovnako sa sprava aj pri statickom planovani. Tu ale vznika problem
 * v tom, ze pri statickom planovani este nie su tie ulohy priradene
 * ku vybranym zdrojom a teda sa takmer vsetky priradia jednemu zdroju.
 *
 * @author alvin
 */
public class FirstSuitableSched implements AbstractGlobalScheduling {
    
    /** Creates a new instance of FirstSuitableSched */
    public FirstSuitableSched() {
        Dbg.vypis("Konstruktor FirstSuitable().");
    }

    public String getName() {
        return "First suitable";
    }

    public int getIndex() {
        return 0;
    }

    public ParametersDialog getParamDialog() {
        return null;
    }

    public Object getParameters() {
        return null;
    }

    public void setParameters(Object parameters) {
    }

    public int findResource(GridletInfo task,Collection<ResourceCharacteristicExt> resources) {
        int result = -1;
        for (ResourceCharacteristicExt element : resources) {
            if(element.isSuitableResource(task)) {
                if (element.hasFreePE()) {
                    return element.getResourceID();
                } else if (result == -1)
                    result = element.getResourceID();
            }
        }
        return result;
    }

    public Vector<Integer> findResources(Vector<GridletInfo> tasks, Collection<ResourceCharacteristicExt> resources) {
        int count = tasks.size();
        Vector<Integer> result = new Vector<Integer>(count);
        for (int i = 0; i < count; i++) {
            result.add(new Integer(findResource(tasks.get(i), resources)));
        }
        return result;
    }

    public Vector<String> getParametersText() {
        return null;
    }

    public void setParametersText(Vector<String> newValues) {
    }

    public void createParamDialog(JFrame owner) {
    }

}
